package cn.christ.com.mobilesafe.view.activity_sjfd;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import butterknife.ButterKnife;
import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.common.IUIOperation;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;

import static android.view.GestureDetector.SimpleOnGestureListener;

/**
 * Created by Administrator on 2018/7/21.
 */

public abstract class BaseSetingUpActivity extends AppCompatActivity implements IUIOperation {

    private GestureDetector detector;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        ButterKnife.bind(this);
        //设置沉浸式
        Global.setNoStatusBarFullMode(this);
        View tvSetingTitle = findViewById(R.id.tv_seting_title);
        if (tvSetingTitle != null) {
            Global.setStatusBarPadding(tvSetingTitle);
        }

        //交给手势识别处理
        detector = new GestureDetector(this, new MGestureDetectorListener());
        initView();
        initData();
        initListener();
    }

    /**
     * 手势识别
     */
    class MGestureDetectorListener extends SimpleOnGestureListener {
        /**
         * 快速滑动
         *
         * @param e1
         * @param e2
         * @param velocityX
         * @param velocityY
         * @return
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //获取x1和x2的值
            int x1 = (int) e1.getX();
            int x2 = (int) e2.getX();

            if (x1 - x2 > 50) {
                next();
                return true;
            }

            if (x2 - x1 > 50) {
                pre();
                return true;
            }

            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }

    /**
     * 前一页
     */
    protected void pre() {
        performPre();
        finish();
        overridePendingTransition(R.anim.pre_enter, R.anim.pre_exit);
    }


    /**
     * 下一页
     */
    protected void next() {
        if (this instanceof SetingUpActivity2) {
            String sim = (String) SPUtils.get(Constants.SJFD_BIND_SIM, "");
            if (TextUtils.isEmpty(sim)) {
                Global.showToast(getString(R.string.bind_not_sim));
                return;
            }
        }
        performNext();
        finish();
        overridePendingTransition(R.anim.next_enter, R.anim.next_exit);
    }

    protected abstract void performPre();

    protected abstract void performNext();

    /**
     * 跳转activity
     *
     * @param clazz
     */
    protected void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转activity,不finish
     *
     * @param clazz
     */
    protected void startActivityNotFinish(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (detector != null) {
            detector = null;
        }
    }
}
